<%= content_for :javascript do %> 
  <script src="/javascripts/app/documents.js"></script>
  <script src="/javascripts/app/documentsAgendaPleno.js"></script>
  <script src="/javascripts/app/documentsAgendaPermanente.js"></script>

<script type="text/javascript">
    
    var addDocumentPopup = null;
    
    $(document).ready(function () {

      $(".save-document").click(function (e) {
        e.preventDefault();
        $("#add-document-frame").contents().find("#new_document").submit();
      });

      $(".save-document-permissions").click(function (e) {
        e.preventDefault();
        $("#view-document-permissions-frame").contents().find(".form-horizontal").submit();
      });
      
        $(".nav-list a").click(function (e) {
          e.preventDefault();
          $(".nav-list li").removeClass("active");
          $(this).parent().addClass("active");
          var categoryId = $(this).attr("data-id");
          $(".search-query").attr("data-id",categoryId);
          App.DocumentController.init({"category": categoryId});
        });
          
      <% @subsections.each do |section| %>
          $(".search-<%= section.id %>").attr("data-id",<%= @categories[section.short_name][:active_category] %>);
          $(".search-<%= section.id %>").keypress(function(e) {            
              var code = (e.keyCode ? e.keyCode : e.which);
              if (code == 13) {
                App.<%= section.api_controller %>.search({
                  "category": $(this).attr("data-id"),
                  "search_query": $(this).val()
                });
              }
          });

          App.<%= section.api_controller %> = new <%= section.api_controller %>({"category": <%= @categories[section.short_name][:active_category] %>});
      <% end %>      
  });  

    function addDocument(categoryId) {
      $("#add-document-frame").contents().find("#document_category_id").val(categoryId);      
      addDocumentPopup = $("#add-document").modal("show");      
    }        

    function deleteDocument(documentId,categoryId) {
      <% @subsections.each do |section| %>
        <% @categories[section.short_name][:categories].each do |category| %>
          if (categoryId == <%=category.id %>) {
              App.<%= section.api_controller %>.deleteDocument(documentId);
          }
        <% end %>
      <% end %>
    }

    function viewPermissions(documentId,categoryId) {
        src = "/documents/permissions/"+documentId;
        $("#view-document-permissions-frame").attr("src",src);
        viewPermissionsPopup = $("#view-document-permissions").modal("show");
    }
  </script>

<% end %>
<div class="page-header">
  <h1>Agendas</h1>
</div>

<% if @subsections.count > 0 %>
    <ul class="nav nav-tabs" id="myTab">     
      <% @subsections.each do |section| %>
        <li class="<%= @active_subsection_id == section.id ? 'active' : '' %>">
            <a href="#<%=section.id%>" data-toggle="tab" class="agenda-comision">
                <%= section.name %>
            </a>
        </li>
      <% end %>
    </ul>

    <div class="tab-content">
      <% @subsections.each do |section| %>
        <div class="tab-pane <%= @active_subsection_id == section.id ? 'active': '' %>" id="<%= section.id %>">
            <%= "No tiene permisos para ver esta categorias" unless @categories[section.short_name][:has_active_categories] %>
            <% if @categories[section.short_name][:has_active_categories] %>
                <div class="row">
                    <% if @categories[section.short_name][:categories].count > 0 %>
                        <% if @categories[section.short_name][:categories].count > 1 %>
                            <div class="span3">        
                              <div class="well" style="padding: 8px 0;">
                                <ul class="nav nav-list">
                                  <% @categories[section.short_name][:categories].each do |category| %>
                                      <li class="<%=  @categories[section.short_name][:active_category] == category.id ? 'active' : ''%> <%= category.category_type == 'title' || category.category_type == 'standard-link' ? 'nav-header' : '' %>">
                                        <% if category.category_type == 'standard' || category.category_type == 'standard-link' %>
                                          <a href="#" data-id="<%= category.id %>"><%= category.name %></a>
                                        <% else %>
                                          <%= category.name %>
                                        <% end %>
                                      </li>
                                      <li class="divider"></li>
                                  <% end %>
                                </ul>
                              </div>
                            </div>
                        <% end %>
                        <div class="<%= @categories[section.short_name][:categories].count > 1 ? 'span9' : 'span12' %>">
                          <div class="pull-right input-append" style="padding-top:30px;">
                            <input type="text" placeholder="Buscar Agenda de comisión..." class="input-xlarge search-query search-<%=section.id%>">
                            </input>
                          </div>
                          <div class="clearfix"></div>
                          <div id="<%= section.section_container %>">
                          </div>
                          <div class="<%= section.section_pagination %> pagination pagination-centered">    
                          </div>          
                        </div>
                    <% end %>
                </div>
            <% end %>
        </div>
      <% end %>
    </div>
<% end %>




<%= content_for :modal do %>  
    <div class="modal hide" id="add-document" style="height:320px;">
      <div>
        <!-- ** widget header ** -->
        <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
          <h4>Agregar documento</h4>
        </div>
        <!-- ** ./ widget header ** -->
        <!-- ** dummy content ** -->
        <iframe id="add-document-frame" src="/documents/new" style="width:100%;border:0px;height:220px;"></iframe>
        <!-- ** ./ dummy content ** -->
      </div>
      <div class="modal-footer">
        <a href="#" class="btn btn-primary save-document">Cargar documento</a> <a href="#" class="btn btn-warning close-owner" data-dismiss="modal">Cerrar</a>
      </div>
    </div>
    <div class="modal hide" id="view-document-permissions" style="height:320px;">
      <div>
        <!-- ** widget header ** -->
        <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
          <h4>Permisos del archivo</h4>
        </div>
        <!-- ** ./ widget header ** -->
        <!-- ** dummy content ** -->
        <iframe id="view-document-permissions-frame" src="" style="width:100%;border:0px;height:220px;"></iframe>
        <!-- ** ./ dummy content ** -->
      </div>
      <div class="modal-footer">
        <a href="#" class="btn btn-primary save-document-permissions">Grabar permisos</a> <a href="#" class="btn btn-warning close-document-permissions" data-dismiss="modal">Cerrar</a>
      </div>
    </div>
<% end %>

<%= content_for :templates do %>
    <script id="documents-template" type="text/x-handlebars-template">
      <section style="padding-top: 0px;">
        <h3>
          <span>{{category_name}}</span>
          <div class="pull-right">
            <% if @current_account.admin? %>
              <a href="#" class="btn btn-primary btn-small add-document" onclick="addDocument({{category_id}})">Añadir documento</a>
            <% end %>
          </div>
        </h3>      
        <div class="clearfix">
          {{#if documents}}
          <table class="table table-bordered table-hover">
            <tr>
              <th>Fecha</th>
              <th>Nombre</th>
              <th>Descripción</th>
              <th>Acciones</th>
            </tr>
            {{#each_with_index documents}}
            {{#with this}}
            <tr>
              <td>{{#formatDate upload_at}}
                j f Y
                {{/formatDate}}
              </td>
              <td>{{name}}</td>
              <td>{{description}}</td>
              <td>
                <a href="{{content_url}}">Descargar</a>
                <% if @current_account.admin? %>
                  | <a href="#" onclick="deleteDocument({{id}},{{category_id}})">Eliminar</a>
                  | <a href="#" onclick="viewPermissions({{id}})">Permisos</a>
                <% end %>
              </td>
            </tr>
            {{/with}}
            {{/each_with_index}}                          
          </table>
          {{/if}}
          {{#unless documents}}No hay documentos que mostrar aun.{{/unless}}
        </div>
      </section>    
    </script>   
    <script id="pagination" type="text/x-handlebars-template">
      <ul>{{{create_pagination_links currentPage perPage totalEntries paginateFunction category_id}}}</ul>
    </script> 
<% end %>
